﻿using CommonCater;
using Lufei_CaterBLL;
using Lufei_CaterModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace ItcastCater
{
    public partial class FrmBalance : Form
    {
        public FrmBalance()
        {
            InitializeComponent();
        }

        public int OrderID { get; set; }//存储OrderID
        public string DeskName { get; set; }//存储桌子编号

        public int DeskID { get; set; }//存储桌子的id



        OrderInfoBLL bll=new OrderInfoBLL();    

        private void FrmBalance_Load(object sender, EventArgs e)
        {
            // 窗体传值---- -》OrderID 桌子编号   消费金额
            labDeskName.Text = this.DeskName;
            labOrderId.Text = this.OrderID.ToString();
            //lblMoney.Text = MoneyAndCount.Money.ToString();
            //labMoney.Text = MoneyAndCount.Money.ToString();
            lblMoney.Text = bll.GetMoney(this.OrderID).ToString();
            labMoney.Text = bll.GetMoney(this.OrderID).ToString();
            LoadMemmbersInfo();
            LoadOrderProduct();
        }


        R_OrderInfo_Product_BLL r_OrderInfo_Product_BLL = new R_OrderInfo_Product_BLL();
        //加载点菜的数据
        private void LoadOrderProduct()
        {
           dgvAllPro.AutoGenerateColumns = false;
           dgvAllPro.DataSource= r_OrderInfo_Product_BLL.GetOrderInfoProducts(this.OrderID);
        }




        MemmverInfoBLL membll = new MemmverInfoBLL();

        /// <summary>
        /// 加载会员信息
        /// </summary>
        private void LoadMemmbersInfo()
        {
            List<MemmberInfo> list= membll.GetMemmverInfo();

            list.Insert(0,new MemmberInfo() { MemName="请选择",MemmberId=-1});
            cmbMemmber.DisplayMember = "MemName";
            cmbMemmber.ValueMember = "MemmberId";
            cmbMemmber.DataSource = list;
        }


        MemmberTyoeBLL memmberTyoeBLL = new MemmberTyoeBLL();

        /// <summary>
        /// 当选择会员用户的时候改变值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbMemmber_SelectedIndexChanged(object sender, EventArgs e)
        {

            //获取下拉框对象
            //请选择不要
            if (cmbMemmber.SelectedIndex==0)
            {
                return;
            }
            else
            {
                //获取下拉框中选择的对象
                int id = Convert.ToInt32(cmbMemmber.SelectedValue);
                MemmberInfo mi=  membll.GetMemberInfoByID(id);
                labyeMoney.Text= mi.MemMoney.ToString();    //用户余额
                lblDis.Text = mi.MemDiscount.ToString();

                //根据memberinfoid 获取  memmbertype
                labTp.Text= memmberTyoeBLL.GetMemmberTypeByID(id).ToString();

                //结账金额  =原价 * 折扣
                lblMoney.Text = (Convert.ToDecimal(labMoney.Text) * Convert.ToDecimal(lblDis.Text) / 10).ToString();
            }
        }


        DeskInfoBLL DeskInfoBLL = new DeskInfoBLL();

        /// <summary>
        /// 结账
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAccounts_Click(object sender, EventArgs e)
        {
            //获取钱
            string str=txtMoney.Text.Trim();
            if (string.IsNullOrEmpty(str))
            {
                MessageBox.Show("请付款!!!!!!");
                log<FrmBalance>.LogDebug("用户没有付款");
                return;
            }

            //获取实付金额
            decimal realMoney = Convert.ToDecimal(txtMoney.Text);
            //获取结账要付的钱
            decimal payMoney = Convert.ToDecimal(lblMoney.Text);


            decimal leftMoney = 0;
            if (cmbMemmber.SelectedIndex!=0)
            {
                decimal yeMoney = Convert.ToDecimal(labyeMoney.Text);
                if (yeMoney < payMoney)
                {
                    MessageBox.Show("结账失败，余额不足!!!!!!");
                    log<FrmBalance>.LogDebug("余额不足，钱不够");
                    return;
                }

                 leftMoney = Convert.ToDecimal(labyeMoney.Text) - Convert.ToDecimal(txtMoney.Text);
            }
            else
            {
                if (realMoney < payMoney)
                {
                    MessageBox.Show("结账失败，请多付一些钱!!!!!!");
                    log<FrmBalance>.LogDebug("结账失败，钱不够");
                    return;

                }
            }



            decimal money = Convert.ToDecimal(str);
            //桌子状态更新
             bool isUpdateDeskState=  DeskInfoBLL.UpdateDeskStateByID(0,this.DeskID);
            //订单状态更新
            //if (cmbMemmber.SelectedIndex!=0)
            //{
            //    //[SubTime] DATE, [Remark] NVARCHAR(128), [OrderState] SMALLINT, [OrderMemId] INTEGER, [DelFlag] SMALLINT, [SubBy] INTEGER, [BeginTime] DATE, [EndTime] DATE, [OrderMoney] DECIMAL, [DisCount] DECIMAL);
            //    //选择会员购买
            //    //更新订单表 OrderState  OrderMemId DelFlag EndTime OrderMoney DisCount
            //}
            //else
            //{
            //    //直接购买
            //    //更新订单表 OrderState  DelFlag EndTime OrderMoney
            //}

            OrderInfo oi = new OrderInfo();
            oi.OrderState = 2;
            oi.OrderMemId = Convert.ToInt32(cmbMemmber.SelectedValue);
            oi.DelFlag = 0;
            oi.EndTime=DateTime.Now;
            oi.OrderMoney = Convert.ToDecimal(lblMoney.Text);
            oi.DisCount = Convert.ToInt32(lblDis.Text!=""? lblDis.Text :"0");
            oi.OrderId = this.OrderID;

           bool isUpteOrder= bll.UpdateOrderData(oi);
            if (isUpteOrder&& isUpdateDeskState)
            {
                MessageBox.Show("结账成功!!! 卡内余额为:"+ leftMoney.ToString());
                lblSpareMoney.Text = (realMoney - payMoney).ToString();
                membll.UpdateMemberInfoMoney(Convert.ToInt32(cmbMemmber.SelectedValue), leftMoney);
            }
            else
            {
                MessageBox.Show("结账失败!!! 卡内余额不足" );
            }
        }
    }
}
